{include file="public/header"/}
<link rel="stylesheet" href="{__PUBLIC_PATH}/admin/pcf/pcf_help.css?v={$version}"/>
<script src="{__PUBLIC_PATH}/common/js/jquery.min.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<style>
@media screen and (max-width:500px){
.layui-layer.layui-layer-page{width:100% !important;overflow:hidden !important;left:0 !important;}
.layui-layer-title{width:100% !important;box-sizing:border-box;}
.layui-layer-content{width:100% !important;}
}
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body table-tool-mini">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline" style="padding-right:110px;">
                    <button id="btnAddUser" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增模型</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
        </div>
    </div>
</div>
<!-- 表格状态列 -->
<script type="text/html" id="tableStateUser">
    <input type="checkbox" name="status" data-id="{{d.id}}" value="{{d.status}}" lay-filter="status" lay-skin="switch" lay-text="是|否" {{d.status==1 ? 'checked':''}}/>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/channel.type/index')}",
            page: true,
            toolbar: false,
            cellMinWidth: 100,
            cols: [[
                {field:'id',title:'ID',width:60},
                {field: 'title', title: '模型名称'},
                {field: 'nid', title: '模型标识'},
                {field: 'ifsystem', title: '类型',templet: function (d) {
                    if (d.ifsystem ==1) {
                        return "系统";
                    }else{
                        return "自定义";
                    }
                }, width: 150},
                {field: 'sort_order', sort: true, title: '排序',width: 100,align: 'center'},
                {field: 'status', templet: '#tableStateUser', title: '状态',width: 100,align: 'center'},
                {align: 'center',title: '操作',templet: function (d) {
                        var html = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                        html += '<a class="layui-btn layui-btn-xs" href="{:url("/channel.field/channel_index")}?channel_id='+d.id+'" style="margin-left:15px;">内容字段</a>';
                        if (d.ifsystem ==1) {
                        html +='<a class="layui-btn layui-btn-danger layui-btn-xs" style="color:#ccc;cursor:not-allowed;background-color:#e6e6e6;margin-left:15px;">删除</a>';
                        } else {
                        html +='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" style="margin-left:15px;">删除</a>';
                        }
                    return html;
                }, width: 200},
            ]]
        });



        // 添加
        $('#btnAddUser').click(function () {
            showEditModel();
        });
        // 工具条点击事件
        table.on('tool(tableUser)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(data.id, data.title);
            }
        });




        // 显示表单弹窗
        function showEditModel(mUser) {
            if(mUser){
                var id = mUser.id;
                $.get('{:url("/channel.type/edit")}?id='+id+'&ajax=1', function(tpl){
                    if(tpl.status){
                        var iframes = layer.open({
                          type: 2,
                          title: '编辑模型',
                          fixed: true, //不固定
                          shadeClose: false,
                          shade: 0.3,
                          content: '{:url("/channel.type/edit")}?id='+id
                        });
                        layer.full(iframes); 
                    }else{
                        layer.msg(tpl.msg, {icon: 2,time:1000});
                    }
                }, 'json');
            }
            else{
                $.get('{:url("/channel.type/add")}?ajax=1', function(tpl){
                    if(tpl.status){
                        var iframes = layer.open({
                          type: 2,
                          title: '新增模型',
                          fixed: true, //不固定
                          shadeClose: false,
                          shade: 0.3,
                          content: '{:url("/channel.type/add")}'
                        });
                        layer.full(iframes);
                    }else{
                        layer.msg(tpl.msg, {icon: 2,time:1000});
                    }
                }, 'json');
            }
        }

        // 删除
        function doDel(id, ntitle) {
            layer.msg('此操作将会删除与该模型所有相关的数据且不可恢复，请谨慎操作！是否确认删除？', {
                btnAlign: 'c',
                time: 0,
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    layer.close(index);
                    $.get("{:url('/channel.type/del')}", {
                        id: id
                    }, function (res) {
                        layer.closeAll('loading');
                        if (res.status) {
                            layer.msg(res.msg, {icon: 1,time:1000});
                            insTb.reload({}, 'data');
                        } else {
                            layer.msg(res.msg, {icon: 2,time:1000});
                        }
                    }, 'json');
                }
            });
        }
        // 开启关闭
        form.on('switch(status)', function(){
            if ($(this).val() == 0){
                $(this).val(1);
            }else{
                $(this).val(0);
            }
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/index/changeTableVal")}';
            changeTableVal('channel_type','id',dataid,'status',this,pcfurl,'channel.Type/index');
        });
    });
</script>
</body>
</html>